package com.bndg.shu.shuyinyangshi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bndg.shu.shuyinyangshi.R;
import com.bndg.shu.shuyinyangshi.bean.MonsterBean;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by r on 17/3/23.
 */

public class MyAdapter extends BaseAdapter {
    private List<MonsterBean> mList;
    private Context context;
    private final LayoutInflater mInflater;
    private ViewHolder holder;

    public MyAdapter(Context context, List<MonsterBean> list) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        this.mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_search, null);
            holder.title = (TextView) convertView.findViewById(R.id.tv_title);
            holder.content = (LinearLayout) convertView.findViewById(R.id.ll_content);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.title.setText(mList.get(position).title);
        LinkedHashMap<String, String> contents = mList.get(position).contents;
        holder.content.removeAllViews();
        for (Map.Entry<String, String> entry : contents.entrySet()) {
            TextView tv = new TextView(context);
            tv.setText(entry.getKey() + " " + entry.getValue());
            tv.setTextSize(13);
            tv.setTextColor(context.getResources().getColor(R.color.White));
            holder.content.addView(tv);
        }
        return convertView;
    }

    private class ViewHolder {
        TextView title;
        private LinearLayout content;

    }
}
